<?php

class Date {
	public $id = '';
	public $title = "";
	public $date = "";
	public $topics = "";
	public $created_from = 0;
	public $created_at = "";
	private $db = null;

	function __construct() {
		$this->db = new Database();
		//print_r($this->db);
 	}

	public function load($id){
    	$this->db->query('SELECT * FROM `dates` WHERE `_id` = \''.$id.'\'');
    	$this->db->moveToFirst();
    	$this->id = $this->db->get("_id");
    	$this->title = $this->db->get("title");
    	$this->date = $this->db->get("date");
    	$this->topics = $this->db->get("topics");
    	$this->created_from = $this->db->get("created_from");
    	$this->created_at = $this->db->get("created_at");
	}
	
	public function create($title,$date,$topics,$from){
		$this->id = '';
		$this->title = $title;
		$this->date = $date;
		$this->topics = $topics;
        $this->created_from = $from;
        $dt = new MyDateTime();
		$this->created_at =  $dt->getFormated('MYSQL_DATETIME');
	}

	public function delete($id = ''){
		if($id == ''){
			$id = $this->id;
		}
		if($id != '') $this->db->delete('dates',array("_id" => $id));
		else echo "is == ''";
	}

	public function save(){
		if($this->id == ''){
			echo 'insert';
			//$this->db->query("INSERT INTO `dates` (`title`,`date`,`topics`,`created_from`,`created_at`) VALUES
			$this->id = $this->db->insert('dates',array("title" => $this->title,
														"date" => $this->date,
														"topics" => $this->topics,
														"created_from" => $this->created_from,
														"created_at" => $this->created_at));

		}else{
			echo 'update';
        	$this->db->update('dates','_id = \''.$this->id.'\'',
										array("title" => $this->title,
											  "date" => $this->date,
											  "topics" => $this->topics,
											  "created_from" => $this->created_from,
											  "created_at" => $this->created_at));
		}
	}
}

?>